package com.zone.vehicle.superapp.view.home.recommend.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.sdk.module.comment.entity.RecommendEntity;
import com.zone.vehicle.lib.view.star.Star;
import com.zone.vehicle.lib.view.utils.ImageUtil;
import com.zone.vehicle.superapp.R;
import com.zone.vehicle.superapp.bean.RecommendBean;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;


public class RecommendAppAdapter extends RecyclerView.Adapter<RecommendAppAdapter.ViewHolder> {

    private RecommendBean recommendBean;

    private View.OnClickListener listener;

    public RecommendAppAdapter(RecommendBean recommendBean, View.OnClickListener listener){
        this.recommendBean = recommendBean;
        this.listener = listener;
    }

    public void setAppList(RecommendBean recommendBean) {
        this.recommendBean = recommendBean;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.fragment_recommond_item, parent, false);
        v.setOnClickListener(listener);
        return new ViewHolder(v);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        AppEntity app = recommendBean.getAppEntityList().get(position);
        RecommendEntity recommendEntity = recommendBean.getRecommendEntityList().get(position);
        if (ObjectUtils.isNotEmpty(app) && ObjectUtils.isNotEmpty(recommendEntity)){
//            Glide.with(holder.itemView.getContext()).load(app.getIcon()).into(holder.icon);
            ImageUtil.load(holder.itemView.getContext(), holder.icon, app.getIcon());
            holder.name.setText(app.getName());

            if (StringUtils.isNotEmpty(recommendEntity.getScore())) {
                holder.score.setText(recommendEntity.getAverageScore());
                holder.star.setMark(Float.valueOf(recommendEntity.getScore()));
            }
            if (StringUtils.isNotEmpty(app.getLabels())){
                String[] labels = app.getLabels().split(",");
                if (labels.length > 0){
                    holder.tag1.setVisibility(View.VISIBLE);
                    holder.tag1.setText(labels[0]);
                }
                if (labels.length > 1){
                    holder.tag2.setVisibility(View.VISIBLE);
                    holder.tag2.setText(labels[1]);
                }
                if (labels.length > 2){
                    holder.tag3.setVisibility(View.VISIBLE);
                    holder.tag3.setText(labels[2]);
                }
            }
            holder.description.setText(recommendEntity.getNickName()+"："+recommendEntity.getContent());
            holder.itemView.setTag(app);
        }
    }

    @Override
    public int getItemCount() {
        if (ObjectUtils.isNotEmpty(recommendBean) && ObjectUtils.isNotEmpty(recommendBean.getAppEntityList())) {
            return recommendBean.getRecommendEntityList().size();
        }else {
            return 0;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final ImageView icon;
        private final TextView name;
        private final Star star;
        private final TextView score;
        private final TextView tag1;
        private final TextView tag2;
        private final TextView tag3;
        private final TextView description;


        public ViewHolder(View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.icon);
            name = itemView.findViewById(R.id.name);
            star = itemView.findViewById(R.id.star);
            score = itemView.findViewById(R.id.score);
            tag1 = itemView.findViewById(R.id.tag1);
            tag2 = itemView.findViewById(R.id.tag2);
            tag3 = itemView.findViewById(R.id.tag3);
            description = itemView.findViewById(R.id.description);
        }
    }

}
